package com.epalmpay.controller.channel.order;

import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.order.*;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.service.order.IOrderGoodService;
import com.epalmpay.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by lifayi on 2017/9/2.
 */
@RestController
@RequestMapping("/merchant/order/mall")
public class OrderGoodController extends BaseController {

    @Resource
    private IOrderGoodService orderGoodService;

    /*
    *   订单列表
    * */
    @RequestMapping("/list")
    public OrderGoodListOutDTO list(MallOrderQueryDTO mallOrderQueryDTO,PageDTO pageDTO) {
        OrderGoodListOutDTO orderGoodListOutDTO = new OrderGoodListOutDTO();

        Page page = Tools.pageEmtity(pageDTO);

        try {
            mallOrderQueryDTO.setShopId(mallOrderQueryDTO.getShopId() == null ? 0L : mallOrderQueryDTO.getShopId());
            List<OrderGoodOutDTO> orderGoodOutDTOList = orderGoodService.listMallOrder(mallOrderQueryDTO, page);
            orderGoodListOutDTO.setOrderGoodOutDTOList(orderGoodOutDTOList);
            orderGoodListOutDTO.setPage(page);
            return orderGoodListOutDTO;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    *  订单导出
    * */
    @RequestMapping("/listToExcel")
    public OrderGoodListOutDTO listToExcel(MallOrderQueryDTO mallOrderQueryDTO,PageDTO pageDTO) {
        OrderGoodListOutDTO orderGoodListOutDTO = new OrderGoodListOutDTO();

        try {


//            mallOrderQueryDTO.setShopId(0L);
            if(mallOrderQueryDTO.getShopId() !=null){

                mallOrderQueryDTO.setShopId(mallOrderQueryDTO.getShopId() == null ? 0L : mallOrderQueryDTO.getShopId());
            }
            List<OrderGoodExcelOutDTO> orderGoodOutDTOList  = orderGoodService.listMallOrderExcel(mallOrderQueryDTO, null);
            orderGoodListOutDTO.setOrderGoodExcelOutDTOList(orderGoodOutDTOList);
            return orderGoodListOutDTO;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    *  订单操作（修改金额  发货等）
    * */
    @RequestMapping("/manipulation")
    public BaseOutDTO manipulation(@RequestBody MallOrderInDTO mallOrderInDTO) {
        try {
            return orderGoodService.saveManipulation(mallOrderInDTO,ROOT_HOME_URL);
        } catch (Exception e) {
            e.printStackTrace();
            BaseOutDTO baseOutDTO = new BaseOutDTO();
            baseOutDTO.setErrorCode(Rescode.FAIL);
            return baseOutDTO;
        }
    }

    /*
    *  订单详情
    * */
    @RequestMapping("/get")
    public OrderGoodOutDTO get(@RequestBody MallOrderQueryDTO mallOrderQueryDTO) {
        return orderGoodService.get(mallOrderQueryDTO);
    }

    /*
    *
    * */
    @RequestMapping("/orderQuery")
    public Map<String, Long> orderQuery(@RequestBody Long orderId) {
        return orderGoodService.orderQuery(orderId);
    }
}
